package cn.sxt.bus.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.bus.domain.Goods;
import cn.sxt.bus.domain.Inport;
import cn.sxt.bus.mapper.GoodsMapper;
import cn.sxt.bus.mapper.InportMapper;
import cn.sxt.bus.service.InportService;
import cn.sxt.bus.vo.GoodsVo;
import cn.sxt.bus.vo.InportVo;
import cn.sxt.sys.utils.DataGridView;

@Service
public class InportServiceImpl implements InportService {

	@Autowired
	private InportMapper inportMapper;

	@Autowired
	private GoodsMapper goodsMapper;

	@Override
	public DataGridView queryAllInport(InportVo inportVo) {
		Page<Object> page = PageHelper.startPage(inportVo.getPage(), inportVo.getLimit());
		List<InportVo> list = inportMapper.queryAllInport(inportVo);
		return new DataGridView(page.getTotal(), list);
	}

	@Override
	public List<InportVo> queryAllInportForList(InportVo inportVo) {
		// TODO Auto-generated method stub
		return inportMapper.queryAllInport(inportVo);
	}

	@Override
	public int deleteInportById(InportVo inportVo) {
		// 得到这个商品的id
		Inport inport = inportMapper.selectByPrimaryKey(inportVo.getId());
		Integer goodsid = inport.getGoodsid();
		// 查这个商品之前的库存
		Goods goods = goodsMapper.selectByPrimaryKey(goodsid);
		Integer oldnum = goods.getNumber();
		// 删除前的进货数量
		Integer beginnum = inport.getNumber();
		// 删除
		int i = inportMapper.deleteByPrimaryKey(inportVo.getId());

		// 再把 进货数量 +到 商品 库存里
		goods.setNumber(oldnum - beginnum);
		int j = goodsMapper.updateByPrimaryKeySelective(goods);
		return 1;
	}

	@Override
	public int addInport(InportVo inportVo) {
		// 先添加进货
		int i = inportMapper.insert(inportVo);

		// -------修改商品的库存----
		// 得到这个商品的id
		Integer goodsid = inportVo.getGoodsid();
		// 查这个商品之前的库存
		Goods goods = goodsMapper.selectByPrimaryKey(goodsid);
		Integer oldnum = goods.getNumber();
		// 得到进货的 数量
		Integer number = inportVo.getNumber();
		// 再把 进货数量 +到 商品 库存里
		goods.setNumber(oldnum + number);
		int j = goodsMapper.updateByPrimaryKeySelective(goods);
		return 1;
	}

	@Override
	public Inport queryInportById(Integer id) {
		// TODO Auto-generated method stub
		return inportMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateInport(InportVo inportVo) {
		// 得到这个商品的id
		Inport inport = inportMapper.selectByPrimaryKey(inportVo.getId());
		Integer goodsid = inport.getGoodsid();
		// 查这个商品之前的库存
		Goods goods = goodsMapper.selectByPrimaryKey(goodsid);
		Integer oldnum = goods.getNumber();
		// 修改前的进货数量
		Integer beginnum = inport.getNumber();
		// 修改进货
		int i = inportMapper.updateByPrimaryKeySelective(inportVo);

		// 得到 修改后 进货的 数量
		Integer number = inportVo.getNumber();
		// 再把 修改后进货数量 +到 商品 库存里
		// 库存 - 修改前的进货量 + 修改后的进货量
		
		goods.setNumber(oldnum - beginnum + number);
		int j = goodsMapper.updateByPrimaryKeySelective(goods);
		return 1;
	}

	@Override
	public InportVo queryInportVoById(Integer id) {
		// TODO Auto-generated method stub
		return inportMapper.queryInportVoById(id);
	}

}
